using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Security.Cryptography;

/// <summary>
/// Summary description for Password
/// </summary>
namespace BHYT.Server
{
    public static class Password
    {
        #region Constants

        private const int SaltLength = 4;

        #endregion

        #region Methods

        public static byte[] CreatePassword(string password)
        {
            // Create the unsalted password hash.
            byte[] UnsaltedPassword = CreatePasswordHash(password);

            // Generate a random salt value.
            byte[] SaltValue = new byte[SaltLength];
            RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider();
            Rng.GetBytes(SaltValue);

            // Create the salted hash.
            return CreateSaltedPassword(SaltValue, UnsaltedPassword);
        }

        private static byte[] CreatePasswordHash(string password)
        {
            SHA1Managed Sha1 = new SHA1Managed();
            return Sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
        }

        private static byte[] CreateSaltedPassword(byte[] saltValue, byte[] unsaltedPassword)
        {
            // Add the salt to the hash.
            byte[] RawSalted = new byte[unsaltedPassword.Length + saltValue.Length];
            unsaltedPassword.CopyTo(RawSalted, 0);
            saltValue.CopyTo(RawSalted, unsaltedPassword.Length);

            // Create the salted hash.
            SHA1Managed Sha1 = new SHA1Managed();
            byte[] SaltedPassword = Sha1.ComputeHash(RawSalted);

            // Add the salt value to the salted hash.
            byte[] DbPassword = new byte[SaltedPassword.Length + saltValue.Length];
            SaltedPassword.CopyTo(DbPassword, 0);
            saltValue.CopyTo(DbPassword, SaltedPassword.Length);

            return DbPassword;
        }

        public static bool ComparePasswords(byte[] storedPassword, string suppliedPassword)
        {
            // Extract the salt value from the salted hash.
            byte[] SaltValue = new byte[SaltLength];
            int SaltOffset = storedPassword.Length - SaltLength;
            int i = 0;
            for (i = 0; i < SaltLength; i++)
            {
                SaltValue[i] = storedPassword[SaltOffset + i];
            }

            // Convert the password supplied by the user
            // to a salted password, using the salt value
            // from the database record.
            byte[] HashedPassword = CreatePasswordHash(suppliedPassword);
            byte[] SaltedPassword = CreateSaltedPassword(SaltValue, HashedPassword);

            // Compare the two salted hashes.
            // If they are the same, authentication has succeeded.
            return CompareByteArray(storedPassword, SaltedPassword);
        }

        private static bool CompareByteArray(byte[] arrayA, byte[] arrayB)
        {
            // Make sure the arrays are the same size.
            if (arrayA.Length != arrayB.Length)
            {
                return false;
            }

            // Compare each byte in the two arrays.
            int i = 0;
            for (i = 0; i < arrayA.Length; i++)
            {
                if (!(arrayA[i].Equals(arrayB[i])))
                {
                    return false;
                }
            }

            // Both tests succeeded. The arrays match.
            return true;
        }

        #endregion


    }
}

